<template>
  <div class="newsparticulars-contain">
    <div class="text">
      <div class="title">{{ newsContent.title }}</div>
      <div class="explain">
        <span>发布时间：{{ $moment(newsContent.add_time).format("YYYY-MM-DD") }}</span
        ><span>阅读：{{ newsContent.click }}</span>
      </div>
      <hr />
      <div class="content" v-html="newsContent.content"></div>
    </div>
    <div class="footer">
      <textarea placeholder="请友善留言" rows="4" v-model="commit" maxlength="50" @input="limit"></textarea>
      <div class="limit">{{ wordsNum }}/50</div>
      <van-button type="info" @click="publish">评论</van-button>
      <div class="commit" v-for="item in commits" :key="item.id">
        <div>{{ item.user_name }}：{{ item.content }}</div>
        <div>评论时间：{{ $moment(item.add_time).format("YYYY-MM-DD HH:mm:ss") }}</div>
      </div>
      <van-button style="color: red; border-color: red" @click="more">查看更多</van-button>
    </div>
  </div>
</template>

<script>
import { getOneNews, getNewsComments, pubCommit } from "@/api";
export default {
  data() {
    return {
      pageindex: 1,
      newsContent: [],
      commits: [],
      commit: "",
      wordsNum: 0,
    };
  },
  methods: {
    limit() {
      this.wordsNum++;
    },
    async publish() {
      if (this.commit === "") {
        this.$toast("评论不能为空");
        return;
      }
      let { message } = await pubCommit(this.$route.params.id, this.commit);
      this.$toast(message);
      this.commit = "";
      this.wordsNum = 0;
      this.getNewsComments();
    },
    async more() {
      this.pageindex++;
      let { message } = await getNewsComments(this.$route.params.id, this.pageindex);
      if (message.length === 0) {
        this.$toast("没有更多评论");
        return;
      }
      this.commits = [...this.commits, ...message];
    },
    async getOneNews() {
      let { message } = await getOneNews(this.$route.params.id);
      this.newsContent = message;
    },
    async getNewsComments() {
      let { message } = await getNewsComments(this.$route.params.id, this.pageindex);
      this.commits = message;
    },
  },
  mounted() {
    this.getOneNews();
    this.getNewsComments();
  },
};
</script>

<style lang="scss" scoped>
.newsparticulars-contain {
  padding: 10px;
  background: #fff;

  .text {
    line-height: 22px;

    .title {
      margin: 22px 0;
      font-size: 22px;
      font-weight: 700;
      line-height: 30px;
    }

    .explain {
      display: flex;
      justify-content: space-between;
      font-size: 12px;
    }

    .content {
      ::v-deep p {
        margin: 16px 0;
        font-size: 16px;
      }
    }
  }

  .footer {
    textarea {
      width: 100%;
      font-size: 14px;
      border-color: transparent;
    }

    .limit {
      display: flex;
      flex-direction: row-reverse;
      font-size: 12px;
    }

    .van-button {
      width: 100%;
      margin-top: 20px;
    }

    .commit {
      padding: 5px 0;
      margin-top: 10px;
      font-size: 12px;
      line-height: 16px;
      background: #f2f2f2;
    }
  }
}
</style>
